package com.xxg.renrenorder.component;

import com.xxg.renrencommon.domain.bean.UserSpreadBenefitInfo;
import com.xxg.renrencommon.domain.dao.RenrenShopUser;
import com.xxg.renrencommon.domain.filedenum.userenum.BenefitStatusEnum;
import com.xxg.renrenorder.dao.*;
import com.xxg.renrenorder.dao.bean.AfterSalesOrderInfo;
import com.xxg.renrenorder.dao.bean.OrderMasterInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 类说明
 *
 * @author szj
 * @date 2021/12/17
 */
@Service
@Slf4j
@AllArgsConstructor
public class UserBenefitComponentImpl implements UserBenefitComponent{
    private final UserSpreadBenefitInfoDao userSpreadBenefitInfoDao;
    private final RenrenShopShopDao renrenShopShopDao;
    private final RenrenShopUserDao renrenShopUserDao;

    @Override
    public void recordBenefit(OrderMasterInfo orderMasterInfo) {
        log.info("recordBenefit:{}", orderMasterInfo.getId());
        RenrenShopShop renrenShopShop = renrenShopShopDao.selectByPrimaryKey(orderMasterInfo.getShopId());
        RenrenShopUser renrenShopUser = renrenShopUserDao.selectByPrimaryKey(renrenShopShop.getUserId());
        UserSpreadBenefitInfo userSpreadBenefitInfo = new UserSpreadBenefitInfo();
        userSpreadBenefitInfo.setOrderTime(orderMasterInfo.getCreateTime());
        userSpreadBenefitInfo.setUserId(renrenShopShop.getUserId());
        userSpreadBenefitInfo.setShopId(renrenShopShop.getId());
        userSpreadBenefitInfo.setGroupId(renrenShopUser.getGroupId());
        userSpreadBenefitInfo.setOrderType(orderMasterInfo.getOrderType());
        userSpreadBenefitInfo.setOrderId(orderMasterInfo.getId());
        userSpreadBenefitInfo.setAfterSalesOrderId(null);
        userSpreadBenefitInfo.setBenefitStatus(BenefitStatusEnum.UNSETTLED.getValue());
        userSpreadBenefitInfo.setProfit(orderMasterInfo.getTotalPay());
        userSpreadBenefitInfo.setReturnProfit(new BigDecimal("0"));
        userSpreadBenefitInfo.setTotalPrice(orderMasterInfo.getTotalPrice());
        userSpreadBenefitInfo.setReFund(false);
        userSpreadBenefitInfo.setApplyNumber(null);
        userSpreadBenefitInfo.setReturnPay(new BigDecimal("0"));

        userSpreadBenefitInfoDao.insert(userSpreadBenefitInfo);
    }

    @Override
    public boolean autoCommitBenefitStatus(String id) {
        userSpreadBenefitInfoDao.updateBenefitStatus(id);
        return true;
    }

    @Override
    public void recordReFundBenefit(OrderMasterInfo orderMasterInfo, AfterSalesOrderInfo afterSalesOrderInfo) {
        log.info("recordReFundBenefit:{}, {}", orderMasterInfo.getId(), afterSalesOrderInfo.getId());
        RenrenShopShop renrenShopShop = renrenShopShopDao.selectByPrimaryKey(orderMasterInfo.getShopId());
        RenrenShopUser renrenShopUser = renrenShopUserDao.selectByPrimaryKey(renrenShopShop.getUserId());
        UserSpreadBenefitInfo userSpreadBenefitInfo = new UserSpreadBenefitInfo();
        userSpreadBenefitInfo.setOrderTime(orderMasterInfo.getCreateTime());
        userSpreadBenefitInfo.setBenefitStatus(userSpreadBenefitInfoDao.findBenefitStatusByOrderId(orderMasterInfo.getId()));
        userSpreadBenefitInfo.setProfit(new BigDecimal("0"));
        userSpreadBenefitInfo.setReturnProfit(afterSalesOrderInfo.getRealReturnPay().setScale(2, BigDecimal.ROUND_HALF_UP));
        userSpreadBenefitInfo.setReFund(true);
        userSpreadBenefitInfo.setApplyNumber(afterSalesOrderInfo.getNumber());
        userSpreadBenefitInfo.setReturnPay(afterSalesOrderInfo.getRealReturnPay().setScale(2, BigDecimal.ROUND_HALF_UP));
        userSpreadBenefitInfo.setUserId(renrenShopShop.getUserId());
        userSpreadBenefitInfo.setShopId(renrenShopShop.getId());
        userSpreadBenefitInfo.setGroupId(renrenShopUser.getGroupId());
        userSpreadBenefitInfo.setOrderType(orderMasterInfo.getOrderType());
        userSpreadBenefitInfo.setOrderId(orderMasterInfo.getId());
        userSpreadBenefitInfo.setAfterSalesOrderId(afterSalesOrderInfo.getId());
        userSpreadBenefitInfo.setTotalPrice(orderMasterInfo.getTotalPrice());

        userSpreadBenefitInfoDao.insert(userSpreadBenefitInfo);
    }
}
